package com.smartlifev30.baiwei.view.recyclerViewComponent.swiped;

import android.content.Context;
import android.support.annotation.NonNull;

import com.smartlifev30.baiwei.view.recyclerViewComponent.BaseAdapter;
import com.smartlifev30.baiwei.view.recyclerViewComponent.swiped.core.ItemTouchHelperCallback;
import com.smartlifev30.baiwei.view.recyclerViewComponent.swiped.core.ItemTouchHelperExtension;

import java.util.ArrayList;

/**
 * 布局的第一个view是菜单, 第二个view是内容
 * recyclerView.setAdapter(BaseSwipedAdapter)
 * ItemTouchHelperExtension itemTouchHelper = new ItemTouchHelperExtension(new ItemTouchHelperCallback());
 * itemTouchHelper.attachToRecyclerView(recyclerView);
 *
 * @author tys
 * @date 2018/8/24
 */

public abstract class BaseSwipedAdapter<DataType, VH extends BaseSwipedViewHolder> extends BaseAdapter<DataType, VH> {

    private final ItemTouchHelperExtension itemTouchHelper;

    protected BaseSwipedAdapter(Context context, ArrayList<DataType> mData, ItemTouchHelperExtension itemTouchHelper) {
        super(context, mData);
        this.itemTouchHelper = itemTouchHelper;
    }

    protected void closeMenu() {
        itemTouchHelper.closeOpened();
    }


    /**
     * 长按 拖拽移
     * {@link ItemTouchHelperCallback}关闭拖拽功能将不会触发
     *
     * @param from 从
     * @param to   到
     */
    public void onMove(int from, int to) {
        DataType prev = removeData(from);
        addData(to > from ? to - 1 : to, prev);
        notifyItemMoved(from, to);
    }

}
